package cn.edu.cdp.controller;

import cn.edu.cdp.domain.User;
import cn.edu.cdp.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "用户相关接口")
@Slf4j
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户列表接口")
    @GetMapping("/list")
    public String List(Model model){
        log.info("获取数据.....");
        List<User> users = userService.findAll();
        model.addAttribute("list", users);

        return "userPages/list";
    }
    @GetMapping("/user")
    public String  toAddPage() {
        log.info("前往添加页...");
        return "userPages/add";
    }
    @PostMapping("/addUser")
    @ApiOperation("添加用户接口")
    public String addUserController(User user){
        userService.add(user);
//        System.out.println("添加用户");
        log.info("添加用户.....");

        return "redirect:/list";
    }

    @DeleteMapping("/deleteUser/{id}")
    public String deleteUserController(@PathVariable("id") String id){
        log.info("删除用户.....");
        userService.delete(id);
        return "redirect:/list";
    }
}
